/*  
 * cafe-assisttools.js
 * @Author :      GUORUI
 * @CreateDate :  2017年8月22日
 * Copyright (c) 2017, NRIET.　
 * 辅助工具条控件
 */

;
define(function () {
    //创建主界面
    function bulidHtml(cfg) {
        var $warp = $('<div>').addClass("cafe-assisttools");
        $.each([
            [{
                id: "station",
                icon: "tools-1-icon",
                text: "站点选择",
                selFun: function ($c) {
                    var $g = $('<div>').addClass("cafe-at-station");
                    var stations = [
                        { id: "0", type: "aws", defSel: true, text: "沿海站" },
                        { id: "1", type: "aws", defSel: true, text: "海岛站" },
                        { id: "2", type: "aws", defSel: true, text: "浮标站" },
                        { id: "3", type: "aws", defSel: true, text: "船舶站" },
                        { id: "4", type: "aws", defSel: true, text: "平台站" }
                    ];
                    var stationFun = function ($c) {
                        var $content = $("<div>").addClass("cas-content");
                        $.each(stations, function () {
                            var $station = $('<span>').addClass("cas-station").attr({ "sid": this.id, "type": this.type }).text(this.text);
                            if (this.defSel) {
                                $station.addClass("active");
                            }
                            $content.append($station);
                        });
                        $c.append($content);
                    };
                    var $baseStation = $('<div>').addClass("cas-station-select cas-base active");
                    stationFun($baseStation);
                    $g.append($baseStation);
                    $c.append($g.hide());
                }
            },
            {
                id: "geography",
                icon: "tools-2-icon",
                text: "地理信息",
                selFun: function ($c) {
                    var $g = $('<div>').addClass("cafe-at-geography");
                    $.each([
                        { id: "demlayer", isDefSel: true, text: "地形" },
                        { id: "riverlayer", isDefSel: true, text: "水系" },
                        { id: "bianjielayer_noStr", isDefSel: true, text: "边界" },
                        { id: "bianjielayer", isDefSel: false, text: "街道" }
                    ], function () {
                        var $gspan = $('<span>').attr("gid", this.id).text(this.text);
                        if (this.isDefSel) {
                            $gspan.addClass("active");
                        }
                        $g.append($gspan);
                    });
                    $c.append($g.hide());
                }
            },
            { id: "legend", icon: "tools-3-icon", text: "报警图例" },
            { id: "warnmake", icon: "tools-4-icon", text: "预警制作" },
            { id: "warnset", icon: "tools-5-icon", text: "报警设置" },
            { id: "history", icon: "tools-6-icon", text: "历史报警" },
            { id: "typhoon", icon: "tools-8-icon", text: "台风" },
            ],
        ], function (i, o) {
            var $t = $('<div>').addClass("cafe-at-tool");
            $.each(o, function (j, b) {
                var $btn = $('<div>').attr("bid", this.id).addClass("cafe-at-btn").data("bObj", this.id);
                $btn.append($('<i>').addClass(this.icon)).append($('<span>').text(this.text));
                if ($.isFunction(b.selFun)) {
                    b.selFun($btn);
                }
                $t.append($btn);
            });
            $warp.append($t);
        });
        return $warp;
    }
    /**
     * 按钮点击处理
     */
    function btnHandle($that, cfg) {
        switch ($that.data("bObj")) {
            case "geography": //地理信息
                if ($that.hasClass("active")) {
                    $that.find(".cafe-at-geography").show();
                } else {
                    $that.find(".cafe-at-geography").hide();
                }
                break;
            case "station": //站点选择
                if ($that.hasClass("active")) {
                    $that.find(".cafe-at-station").show();
                } else {
                    $that.find(".cafe-at-station").hide();
                }
                break;
            case "enlarge": //放大
                $that.removeClass("active");
                GIS.Extent.Zoom(cfg.gisId, true);
                break;
            case "narrow": //缩小
                $that.removeClass("active");
                GIS.Extent.Zoom(cfg.gisId, false);
                break;
            case "legend": //报警图例
            case "warnmake": //预警制作
            case "history": //历史报警
            case "typhoon":
                $that.removeClass("active");
                if ($.isFunction(cfg.call)) {
                    cfg.call($that.data("bObj"), $that.hasClass("active"));
                }
                break;
        }
    }
    /**
     * 绑定事件
     */
    function _eves($drop, cfg) {
        $drop.off().on("click", ".cafe-at-btn", function () { //选择辅助工具
            if (!$(this).hasClass("disabled")) {
                if ($(this).hasClass("active")) {
                    $(this).removeClass("active");
                } else {
                    $(this).addClass("active");
                }
                debugger;
                btnHandle($(this), cfg);
            }
        }).on("click", ".cas-station", function (e) { //站点选择
            e.stopPropagation();
            if ($(this).hasClass("active")) {
                $(this).removeClass("active");
            } else {
                $(this).addClass("active");
            }
            if (cfg.stationSelFun && cfg.stationSelFun.length > 0) {
                var stationid = $(this).attr("sid");
                var stype = $(this).attr("type");
                var isSel = $(this).hasClass("active");
                cfg.stationSelFun.forEach(function (f) {
                    f(stationid, isSel, stype);
                });
            }
        }).on("click", ".cafe-at-geography>span", function (e) { //地理信息
            e.stopPropagation();
            if ($(this).hasClass("active")) {
                $(this).removeClass("active");
            } else {
                $(this).addClass("active");
            }
            if ("demlayer" == $(this).attr("gid")) {
                GIS.LayerControll.showControll(cfg.gisId, "baselayer", !$(this).hasClass("active"));
            } else if ($(this).hasClass("active") && "bianjielayer" == $(this).attr("gid")) {
                $(".cafe-at-geography span[gid='bianjielayer_noStr']").removeClass("active");
                GIS.LayerControll.showControll(cfg.gisId, "bianjielayer_noStr", false);
            } else if ($(this).hasClass("active") && "bianjielayer_noStr" == $(this).attr("gid")) {
                $(".cafe-at-geography span[gid='bianjielayer']").removeClass("active");
                GIS.LayerControll.showControll(cfg.gisId, "bianjielayer", false);
            }
            GIS.LayerControll.showControll(cfg.gisId, $(this).attr("gid"), $(this).hasClass("active"));
        })
    }
    var containerHeight = 0;
    var CFAssisttools = function (settings) {
        var options = {
            appendTo: null, //控件所在容器
            gisId: null, //GIS的ID
            getCurDate: null, //获取当前时间，仅快视图使用
            call: null //控件点击事件
        };
        this.config = $.extend({}, options, settings);

        var $container = $(this.config.appendTo);
        containerHeight = $container.height();
        this.$drop = bulidHtml(this.config);
        _eves(this.$drop, this.config);

        if (this.config.appendTo) {
            $container.show().empty().append(this.$drop);
        }
    }

    CFAssisttools.prototype = {
        disabledBtn: function (bids) {
            this.$drop.find(".cafe-at-btn").each(function () {
                if (-1 != $.inArray($(this).attr("bid"), bids)) {
                    $(this).removeClass("active").addClass("disabled");
                }
            });
        },
        activeBtn: function (bids, isNoSelected) {
            this.$drop.find(".cafe-at-btn").each(function () {
                if (-1 != $.inArray($(this).attr("bid"), bids)) {
                    $(this).removeClass("disabled");
                    if (!isNoSelected) $(this).addClass("active");
                }
            });
        },
        removeActive: function (bids) {
            this.$drop.find(".cafe-at-btn").each(function () {
                if (-1 != $.inArray($(this).attr("bid"), bids)) {
                    $(this).removeClass("active");
                }
            });
        },
        getArea: function () {
            var $area = this.$drop.find(".cafe-at-zoneswitch>span.active");
            return {
                areaCode: $area.attr("areacode"),
                areaId: $area.attr("areaid")
            };
        },
        getStationType: function (type) {
            var st = [];
            this.$drop.find(".cafe-at-station .cas-station.active").each(function () {
                if (type == $(this).attr("type")) {
                    st.push($(this).attr("sid"));
                }
            });
            return st.join(",");
        },
        setStationType: function (type) {
            this.$drop.find(".cafe-at-station .cas-station[type$='" + type + "']").show().siblings(".cas-station:not(span[type$='" + type + "'])").hide();
        },
        setStationSelFun: function (fun) {
            if (!this.config.stationSelFun) {
                this.config.stationSelFun = [];
            }
            this.config.stationSelFun.push(fun);
        },
        setAreaSelFun: function (fun) {
            this.config.areaSelFun = fun;
        },
        hideDemlayer: function () {
            var $demlayer = this.$drop.find(".cafe-at-geography>span[gid='demlayer']");
            if ($demlayer.hasClass("active")) {
                $demlayer.click();
            }
        }
    };

    CafeJS.Assisttools = function (settings) {
        var drop = new CFAssisttools(settings);
        return drop;
    }
});